package ihm.view;

import ihm.model.DebtModel;

import java.awt.Graphics;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import domain.Debt;

@SuppressWarnings("serial")
public class DebtsPanel extends JPanel implements View, ChangeListener {
	
	private DebtModel model;
	private JTable jTable;
	private String[] columnNames;
	
	public DebtsPanel(DebtModel model) {
		this.model = model;
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		this.columnNames = new String[8];
		columnNames[0] = "#";
		columnNames[1] = "Done";
		columnNames[2] = "Amount";
		columnNames[3] = "Owed to";
		columnNames[4] = "Source";
		columnNames[5] = "Date";
		columnNames[6] = "Date done";
		columnNames[7] = "Comment";

		String[][] rowData = new String[model.getDebts().size()][8];
		int i = 0;
		for (Debt debt : model.getDebts()) {
			rowData[i][0] = debt.getId()+"";
			rowData[i][1] = debt.isDone()+"";
			rowData[i][2] = debt.getAmount()+"";
			rowData[i][3] = debt.getOwedTo().getName();
			rowData[i][4] = debt.getSource().getName();
			rowData[i][5] = debt.getDate();
			rowData[i][6] = debt.getDateDone();
			rowData[i][7] = debt.getComment();
		}
		
		this.jTable = new JTable(rowData, columnNames);
		this.add(jTable);
	}

	@Override
	public void stateChanged(ChangeEvent ce) {
		this.repaint();
	}

}
